package com.punai.customer.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.punai.customer.domain.CusSubpkChapter;
import com.punai.customer.domain.bo.MergeGxToFeeResultBo;
import com.punai.customer.mapper.CusSubpkChapterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.customer.mapper.CusSubpkItemMapper;
import com.punai.customer.domain.CusSubpkItem;
import com.punai.customer.service.ICusSubpkItemService;

/**
 * 工序费用项Service业务层处理
 *
 * @author zxw
 * @date 2022-12-04
 */
@Service
public class CusSubpkItemServiceImpl extends ServiceImpl<CusSubpkItemMapper,CusSubpkItem> implements ICusSubpkItemService
{
    @Autowired
    private CusSubpkItemMapper cusSubpkItemMapper;
    @Autowired
    private CusSubpkChapterMapper cusSubpkChapterMapper;
    /**
     * 查询工序费用项
     *
     * @param id 工序费用项主键
     * @return 工序费用项
     */
    @Override
    public CusSubpkItem selectCusSubpkItemById(String id)
    {
        return cusSubpkItemMapper.selectCusSubpkItemById(id);
    }

    /**
     * 查询工序费用项列表
     *
     * @param cusSubpkItem 工序费用项
     * @return 工序费用项
     */
    @Override
    public List<CusSubpkItem> selectCusSubpkItemList(CusSubpkItem cusSubpkItem)
    {
        Long paramCompId = cusSubpkItem.getParamCompId();
        String chapterId = cusSubpkItem.getChapterId();
        boolean chapterExist = false;
        List<String> chapterIds = new ArrayList<>();
        if (StrUtil.isNotEmpty(chapterId)) {
            chapterExist = true;
            List<CusSubpkChapter> children = getChapterChildren(chapterId, paramCompId);
            chapterIds.add(chapterId);
            if (CollUtil.isNotEmpty(children)) {
                List<String> childrenIds = children.stream().map(CusSubpkChapter::getId).collect(Collectors.toList());
                chapterIds.addAll(childrenIds);
            }
        }
        LambdaQueryWrapper<CusSubpkItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(chapterExist,CusSubpkItem::getChapterId,chapterIds);
        queryWrapper.eq(CusSubpkItem::getCompanyId,paramCompId);
        queryWrapper.orderBy(true,true,CusSubpkItem::getOrderNum);
        return cusSubpkItemMapper.selectList(queryWrapper);
    }

    private List<CusSubpkChapter> getChapterChildren(String chapterId,Long companyId) {
        return cusSubpkChapterMapper.getChildrenById(chapterId,companyId);
    }

    /**
     * 新增工序费用项
     *
     * @param cusSubpkItem 工序费用项
     * @return 结果
     */
    @Override
    public int insertCusSubpkItem(CusSubpkItem cusSubpkItem)
    {
            return cusSubpkItemMapper.insert(cusSubpkItem);
    }

    /**
     * 修改工序费用项
     *
     * @param cusSubpkItem 工序费用项
     * @return 结果
     */
    @Override
    public int updateCusSubpkItem(CusSubpkItem cusSubpkItem)
    {
        return cusSubpkItemMapper.updateById(cusSubpkItem);
    }

    /**
     * 批量删除工序费用项
     *
     * @param ids 需要删除的工序费用项主键
     * @return 结果
     */
    @Override
    public int deleteCusSubpkItemByIds(String[] ids)
    {
        return cusSubpkItemMapper.deleteCusSubpkItemByIds(ids);
    }

    /**
     * 删除工序费用项信息
     *
     * @param id 工序费用项主键
     * @return 结果
     */
    @Override
    public int deleteCusSubpkItemById(String id)
    {
        return cusSubpkItemMapper.deleteCusSubpkItemById(id);
    }

}
